package org.dromara.xhlj.crud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dromara.common.satoken.utils.LoginHelper;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xhlj.domain.vo.XhljDiscussionLikeVo;
import org.dromara.xhlj.domain.query.XhljDiscussionLikeQuery;
import org.dromara.xhlj.domain.bo.XhljDiscussionLikeBo;
import org.dromara.xhlj.crud.service.IXhljDiscussionLikeCrudService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * xhlj-crud-讨论点赞
 *
 * @author yf
 * @date 2024-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xhlj/discussionLike")
public class XhljDiscussionLikeCrudController extends BaseController {

    private final IXhljDiscussionLikeCrudService xhljDiscussionLikeService;

    /**
     * 分页查询讨论点赞列表
     */
    @SaCheckPermission("xhlj:discussionLike:list")
    @GetMapping("/list")
    public TableDataInfo<XhljDiscussionLikeVo> list(XhljDiscussionLikeQuery query, PageQuery pageQuery) {
         return xhljDiscussionLikeService.queryPageList(query, pageQuery);
    }

    /**
     * 查询讨论点赞列表
     */
    @SaCheckPermission("xhlj:discussionLike:list")
    @GetMapping("/allList")
    public TableDataInfo<XhljDiscussionLikeVo> allList(XhljDiscussionLikeQuery query) {
        List<XhljDiscussionLikeVo> list =  xhljDiscussionLikeService.queryList(query);
        return TableDataInfo.build(list);
    }


    /**
     * 导出讨论点赞列表
     */
    @SaCheckPermission("xhlj:discussionLike:export")
    @Log(title = "讨论点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XhljDiscussionLikeQuery query, HttpServletResponse response) {
        List<XhljDiscussionLikeVo> list = xhljDiscussionLikeService.queryList(query);
        ExcelUtil.exportExcel(list, "讨论点赞", XhljDiscussionLikeVo.class, response);
    }

    /**
     * 获取讨论点赞详细信息
     *
     * @param discussionLikeId 主键
     */
    @SaCheckPermission("xhlj:discussionLike:query")
    @GetMapping("/{discussionLikeId}")
    public R<XhljDiscussionLikeVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer discussionLikeId) {
        return R.ok(xhljDiscussionLikeService.queryById(discussionLikeId));
    }

    /**
     * 新增讨论点赞
     */
    @SaCheckPermission("xhlj:discussionLike:add")
    @Log(title = "讨论点赞", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XhljDiscussionLikeBo bo) {
        bo.setCreateBy(Math.toIntExact(LoginHelper.getUserId()));
        return toAjax(xhljDiscussionLikeService.insertByBo(bo));
    }

    /**
     * 修改讨论点赞
     */
    @SaCheckPermission("xhlj:discussionLike:edit")
    @Log(title = "讨论点赞", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XhljDiscussionLikeBo bo) {
        return toAjax(xhljDiscussionLikeService.updateByBo(bo));
    }

    /**
     * 删除讨论点赞
     *
     * @param discussionLikeIds 主键串
     */
    @SaCheckPermission("xhlj:discussionLike:remove")
    @Log(title = "讨论点赞", businessType = BusinessType.DELETE)
    @DeleteMapping("/{discussionLikeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] discussionLikeIds) {
        return toAjax(xhljDiscussionLikeService.deleteWithValidByIds(List.of(discussionLikeIds), true));
    }
}
